package org.example.io.reactor;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Handler implements Runnable {
    SocketChannel channe;

    public Handler(SocketChannel accept) {
        this.channe = accept;
    }

    @Override
    public void run() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int len = 0, total = 0;
        StringBuilder msg = new StringBuilder();
        try {
            do {
                len = channe.read(buffer);
                if (len > 0) {
                    total += len;
                    msg.append(new String(buffer.array()));
                }
            } while (len > buffer.capacity());

            System.out.println(channe.getRemoteAddress() + ": Server receive Msg:" + msg);
            System.out.println("total:" + total);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
